/*******************************************************************************
 *	                           bsp cpu.c
 *******************************************************************************/
#include "bsp.h"
#include "dataproc.h"

/**
 * @brief  Wait For Interrupt
 * @param  none
 * @retval None
 */
void wfi_set(void)
{
  __WFI();
}

/**
 * @brief  关闭中断
 * @param  none
 * @retval None
 */
void _intx_disable(void)
{
  __disable_irq();
}

/**
 * @brief  开启中断
 * @param  none
 * @retval None
 */
void _intx_enable(void)
{
  __enable_irq();
}

/**
 * @brief  nvic irqn set
 * @param  iIRQn 中断号
 * @param  priority 中断优先级
 * @param  newst 中断开关
 * @retval None
 */
void nvic_irq_set(uint32_t iIRQn, uint32_t priority, uint8_t newst)
{
  intx_alloc();

  IRQn_Type IRQn = (IRQn_Type)iIRQn;

  /* disable interrupt */
  intx_disable();

  if (newst)
  {
    NVIC_SetPriority(IRQn, priority);
    NVIC_ClearPendingIRQ(IRQn);
    NVIC_EnableIRQ(IRQn);
  }
  else
  {
    NVIC_SetPriority(IRQn, priority);
    NVIC_ClearPendingIRQ(IRQn);
    NVIC_DisableIRQ(IRQn);
  }

  intx_enable();
}

/**
 * @brief  获取调试状态
 * 支持M3、M4、M7、M23、M33、M35、M55、M85
 * @param  none
 * @retval None
 */
int cpu_dbg_st_get(void)
{
  uint32_t addr;
  addr = code_add3_u32(0xE000EDF0UL - 0x45454545UL - 0x36363636, 0x45454545UL, 0x36363636);
  return (inpw(addr) & 1UL);
}
